﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.DataTool.SqlLite;
using System;
using System.Data;

namespace SinaImgUpload.SainTool.DataEnhance.Sqlite
{
    internal class PicBedManger
    {
        public static string PicBedDB { get; set; } = "Picbed";
        public static string TableName { get; set; } = "Articleinfo";

        public void InitSqllite()
        {
            new SqlliteHelper().InitDataBasePath();
            new SqlliteHelper().ExitDabaBases(PicBedDB, true);
            InitTable();
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        public void InitTable()
        {
            new SqlliteHelper().ExecuteNonQuery(PicBedDB, string.Format("CREATE TABLE {0} (Id INTEGER PRIMARY KEY AUTOINCREMENT,PixivLink TEXT, SinaLink TEXT ,PixivShield Text)", TableName));
        }

        /// <summary>
        /// 检查数据是否存在，若不存在则更新
        /// </summary>
        /// <param name="PicLink"></param>
        /// <param name="SinaLink"></param>
        /// <param name="MoveStaue"></param>
        public void UpdateValue(string PicLink, string SinaLink, string MoveStaue)
        {
            if (ExitsSinaImg(SinaLink))
            {
                string UpdateCommand =
                  $"UPDATE {TableName} SET PixivShield = '{MoveStaue}' WHERE SinaLink = '{FileNameHelper.GetFileNameWithoutExtension(SinaLink)}'";
                Console.WriteLine(UpdateCommand);
                new SqlliteHelper().ExecuteNonQuery(PicBedDB, UpdateCommand);
            }
            else
                InstertValue(PicLink, SinaLink, MoveStaue);
        }

        /// <summary>
        /// 插入处理到的数据
        /// </summary>
        /// <param name="PicLink"></param>
        /// <param name="SinaLink"></param>
        /// <param name="MoveStaue"></param>
        public void InstertValue(string PicLink, string SinaLink, string MoveStaue)
        {
            PicLink = PicLink.Replace("https://i.pximg.net/img-original/img/", "");
            PicLink = PicLink.Replace("https://static.zerochan.net/", "");
            SinaLink = FileNameHelper.GetFileNameWithoutExtension(SinaLink);
            string Insert = "INSERT INTO {0} VALUES(null,'{1}','{2}','{3}')";
            Console.WriteLine(string.Format(Insert, TableName, PicLink, SinaLink, MoveStaue));
            new SqlliteHelper().ExecuteNonQuery(PicBedDB, string.Format(Insert, TableName, PicLink, SinaLink, MoveStaue));
        }

        /// <summary>
        /// 判断新浪图是否在数据库中
        /// </summary>
        /// <param name="SinaImgLink"></param>
        /// <returns></returns>
        public bool ExitsSinaImg(string SinaImgLink)
        {
            DataTable dataTable = SelectSina(SinaImgLink);

            return DataExits(dataTable);
        }

        /// <summary>
        /// 复用的判断
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        private static bool DataExits(DataTable dataTable)
        {
            if (dataTable.Rows.Count > 0)
                return true;
            return false;
        }

        /// <summary>
        /// P站图片是否存在
        /// </summary>
        /// <param name="PixivLink"></param>
        /// <returns></returns>
        public bool ExitsPixivImg(string PixivLink)
        {
            DataTable dataTable = SelectPicLink(PixivLink);

            return DataExits(dataTable);
        }

        /// <summary>
        /// 选择Sina内容
        /// </summary>
        /// <param name="PixivLink"></param>
        /// <returns></returns>
        private DataTable SelectSina(string SinaImgLink)
        {
            SinaImgLink = FileNameHelper.GetFileNameWithoutExtension(SinaImgLink);
            return SelectHelper(SinaImgLink, "SinaLink");
        }

        /// <summary>
        /// 查询图片地址，看是否已有缓存
        /// </summary>
        /// <param name="PicLink"></param>
        /// <returns></returns>
        private DataTable SelectPicLink(string PicLink)
        {
            PicLink = PicLink.Replace("https://i.pximg.net/img-original/img/", "");

            PicLink = PicLink.Replace("https://static.zerochan.net/", "");
            return SelectHelper(PicLink, "PixivLink");
        }

        /// <summary>
        /// 选择数据库内容
        /// </summary>
        /// <param name="WhereValue"></param>
        /// <param name="WhereRowName"></param>
        /// <returns></returns>
        private static DataTable SelectHelper(string WhereValue, string WhereRowName)
        {
            string Select = "SELECT * FROM  Articleinfo WHERE {0}='{1}'";
            Console.WriteLine(string.Format(Select, WhereRowName, WhereValue));
            DataTable dataTable = new SqlliteHelper().ExecuteReader(PicBedDB, string.Format(Select, WhereRowName, WhereValue));
            return dataTable;
        }

        /// <summary>
        /// 查看新浪图片是否失效  -1为无结果 0为失效 1为正常
        /// </summary>
        /// <param name="SinaImgLink"></param>
        /// <returns></returns>
        public int SinaImgInvalid(string SinaImgLink)
        {
            DataTable dataTable = SelectSina(SinaImgLink);
            int ImgExit = -1;
            foreach (DataRow item in dataTable.Rows)
            {
                string ImgShield = item["PixivShield"].ToString();

                switch (ImgShield)
                {
                    case "True":
                        ImgExit = 0;
                        break;

                    case "False":
                        ImgExit = -1;
                        break;

                    case "exits":
                        ImgExit = 1;
                        break;
                }
            }
            return ImgExit;
        }

        /// <summary>
        /// 获取图床链接
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <returns></returns>
        public string GetPicBedLink(string ImgLink)
        {
            DataTable dataTable = SelectPicLink(ImgLink);
            foreach (DataRow item in dataTable.Rows)
            {
                string SinaTempLink = item["SinaLink"].ToString();
                if (SinaTempLink != "")
                    return SinaTempLink;
            }
            return "";
        }
    }
}